package com.design.pattern.create.singleton;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 单例模式(饿汉式 DCL)
 *
 * @author gyf
 * @create 2018-12-01 14:40
 **/
public class SingleCount {
    private AtomicInteger atomicInteger = new AtomicInteger(0);

    private static SingleCount instance;

    private SingleCount() {

    }

    public static SingleCount getInstance() {
        if (instance == null) {
            synchronized (SingleCount.class) {
                if (instance == null) {
                    instance = new SingleCount();
                }
            }
        }
        return instance;
    }

    public int plus() {
        return atomicInteger.incrementAndGet();
    }

    public int decrease() {
        return atomicInteger.decrementAndGet();
    }

    public void showMessage() {
        System.out.println(String.format("当前人数:%d", atomicInteger.get()));
    }
}
